package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.CarDictData;
import org.example.service.CarDictDataService;

import java.util.List;

public class CarDictDataListener implements ReadListener<CarDictData> {

    private static final int BACH_COUNT=3;
    private List<CarDictData> cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
    private CarDictDataService carDictDataService;
    public CarDictDataListener(CarDictDataService carDictDataService) {
        this.carDictDataService=carDictDataService;

    }

    @Override
    public void invoke(CarDictData carDictData, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据:"+carDictData);
        cacheList.add(carDictData);
        if(cacheList.size()>=BACH_COUNT){

            carDictDataService.insertBath(cacheList);
            cacheList=ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("解析完成");
        if(cacheList.size()>=1){
            System.out.println("list:"+cacheList);
            carDictDataService.insertBath(cacheList);
        }
    }
}
